﻿'
' DKP for DotNetNuke® - http://dkpdnn.codeplex.com
' Copyright (c) 2008
' by Casey Lengacher ( http://dkpdnn.codeplex.com )
'
' DotNetNuke®, DNN®, and the DotNetNuke logo are trademarks of DotNetNuke Corporation.
'
' Permission is hereby granted, free of charge, to any person obtaining a copy
' of this software and associated documentation files (the "Software"), to deal
' in the Software without restriction, including without limitation the rights
' to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
' copies of the Software, and to permit persons to whom the Software is
' furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in
' all copies or substantial portions of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
' SOFTWARE.
'

Imports DotNetNuke.Common.Utilities

Namespace DotNetNuke.Modules.tss_DKP

    Partial Public Class tss_ucBoards

        Inherits Entities.Modules.PortalModuleBase

#Region "Private Variables"

        Private Enum PageState As Integer
            NotSet = -1
            View = 0
            Add = 1
            SingleUpdate = 2
            MultiUpdate = 3
        End Enum

#End Region

#Region "Private Properties"

        Private Property vsPageSize() As Integer
            Get
                If ViewState("dkpdnn_BoardPageSize") Is Nothing Then
                    If (Request.Cookies.Get("dkpdnn_BoardPageSize") Is Nothing) Then
                        Return ddlPageSize.Items(0).Value
                    Else
                        Return CInt(Request.Cookies.Get("dkpdnn_BoardPageSize").Value)
                    End If
                Else
                    Return CInt(ViewState("dkpdnn_BoardPageSize"))
                End If
            End Get
            Set(ByVal Value As Integer)
                ViewState("dkpdnn_PlayerPageSize") = Value
                Response.Cookies("dkpdnn_BoardPageSize").Expires = DateTime.Now.AddYears(10)
                Response.Cookies("dkpdnn_BoardPageSize").Value = Value
            End Set
        End Property

        Private Property vsSortExpression() As String
            Get
                If (ViewState("tss_BoardSortExpression") Is Nothing) Then
                    If (Request.Cookies.Get("tss_BoardSortExpression") Is Nothing) Then
                        Return "BoardName"
                    Else
                        Return CStr(Request.Cookies.Get("tss_BoardSortExpression").Value)
                    End If
                Else
                    Return CStr(ViewState("tss_BoardSortExpression"))
                End If
            End Get
            Set(ByVal Value As String)
                ViewState("tss_BoardSortExpression") = Value
                Response.Cookies("tss_BoardSortExpression").Expires = DateTime.Now.AddYears(10)
                Response.Cookies("tss_BoardSortExpression").Value = Value
            End Set
        End Property

        Private Property vsSortDirection() As String
            Get
                If (ViewState("tss_BoardSortDirection") Is Nothing) Then
                    If (Request.Cookies.Get("tss_BoardSortDirection") Is Nothing) Then
                        Return "ASC"
                    Else
                        Return CStr(Request.Cookies.Get("tss_BoardSortDirection").Value)
                    End If
                Else
                    Return CStr(ViewState("tss_BoardSortDirection"))
                End If
            End Get
            Set(ByVal Value As String)
                ViewState("tss_BoardSortDirection") = Value
                Response.Cookies("tss_BoardSortDirection").Expires = DateTime.Now.AddYears(10)
                Response.Cookies("tss_BoardSortDirection").Value = Value
            End Set
        End Property

        Private Property vsCurrentPage() As Integer
            Get
                If (ViewState("tss_BoardCurrentPage") Is Nothing) Then
                    Return 1
                Else
                    Return CInt(ViewState("tss_BoardCurrentPage"))
                End If
            End Get
            Set(ByVal Value As Integer)
                ViewState("tss_BoardCurrentPage") = Value
            End Set
        End Property

        Private Property vsTotalPages() As Integer
            Get
                If (ViewState("tss_BoardTotalPages") Is Nothing) Then
                    Return 1
                Else
                    Return CInt(ViewState("tss_BoardTotalPages"))
                End If
            End Get
            Set(ByVal Value As Integer)
                ViewState("tss_BoardTotalPages") = Value
            End Set
        End Property

        Private Property vsPageState() As PageState
            Get
                If (ViewState("tss_BoardPageState") Is Nothing) Then
                    Return PageState.NotSet
                Else
                    Return CStr(ViewState("tss_BoardPageState"))
                End If
            End Get
            Set(ByVal Value As PageState)
                ViewState("tss_BoardPageState") = Value
            End Set
        End Property

#End Region

#Region "Private Methods"

        Private Sub BindPage()

            Dim tss_DKP As New tss_DataController
            Dim intRowsAffected As Integer
            Dim intPageNumber As Integer = vsCurrentPage
            Dim dblPagesAffected As Double = 1

            ddlPageSize.SelectedValue = ddlPageSize.Items.FindByText(vsPageSize).Value

            grdBoards.PageSize = vsPageSize

            For Each currentColumn As DataGridColumn In grdBoards.Columns
                currentColumn.HeaderText = currentColumn.HeaderText.Substring(0, _
                    IIf(currentColumn.HeaderText.IndexOf(" <img") > 0, currentColumn.HeaderText.IndexOf(" <img"), currentColumn.HeaderText.Length))
                If (currentColumn.SortExpression = vsSortExpression) Then
                    If (vsSortDirection = "ASC") Then
                        currentColumn.HeaderText = currentColumn.HeaderText _
                            + " <img src='/DesktopModules/tss_DKP/Images/grid_icon_uparrow.png' border=0 height=10 width=10>"
                    Else
                        currentColumn.HeaderText = currentColumn.HeaderText _
                            + " <img src='/DesktopModules/tss_DKP/Images/grid_icon_downarrow.png' border=0 height=10 width=10>"
                    End If
                End If
            Next

            grdBoards.DataSource = tss_DKP.tss_GetBoardsByPage(Me.ModuleConfiguration.PortalID, intPageNumber, grdBoards.PageSize, vsSortExpression, vsSortDirection, intRowsAffected)
            grdBoards.DataBind()

            If (grdBoards.Items.Count = 0) And (intPageNumber > 1) Then
                intPageNumber = intPageNumber - 1
                vsCurrentPage = intPageNumber.ToString()
                grdBoards.DataSource = tss_DKP.tss_GetBoardsByPage(Me.ModuleConfiguration.PortalID, intPageNumber, grdBoards.PageSize, vsSortExpression, vsSortDirection, intRowsAffected)
                grdBoards.DataBind()
            End If

            dblPagesAffected = System.Math.Ceiling(intRowsAffected / grdBoards.PageSize)
            If (dblPagesAffected < 1) Then
                dblPagesAffected = 1
            End If
            vsTotalPages = dblPagesAffected.ToString()
            lblCurrentPage.Text = vsCurrentPage
            lblTotalPages.Text = vsTotalPages

            If (intPageNumber = 1) Then
                lnkPreviousPage.Enabled = False

                If (dblPagesAffected > 1) Then
                    lnkNextPage.Enabled = True
                    lnkLastPage.Enabled = True
                Else
                    lnkNextPage.Enabled = False
                    lnkLastPage.Enabled = False
                End If
            Else
                lnkPreviousPage.Enabled = True

                If (intPageNumber = dblPagesAffected) Then
                    lnkNextPage.Enabled = False
                    lnkLastPage.Enabled = False
                Else
                    lnkNextPage.Enabled = True
                    lnkLastPage.Enabled = True
                End If
            End If

            If (DotNetNuke.Security.PortalSecurity.IsInRoles(Me.ModuleConfiguration.AuthorizedEditRoles)) And Not (UserId = -1) Then
                pnlEditPanel.Visible = True
                grdBoards.Columns(0).Visible = True
            Else
                pnlEditPanel.Visible = False
                grdBoards.Columns(0).Visible = False
            End If

        End Sub

        Private Sub ManagePageState(ByVal PageState As PageState)

            Dim tss_DKP As New tss_DataController

            vsPageState = PageState

            If (PageState = PageState.View) Then
                grdBoards.Enabled = True
                BindPage()
            Else
                grdBoards.Enabled = False
            End If

            If (PageState = PageState.View) Then
                pnlEditPanelForm.Visible = False
            Else

                ddlGameID.AutoPostBack = True
                ddlGameID.DataTextField = "GameName"
                ddlGameID.DataValueField = "GameID"
                ddlGameID.DataSource = tss_DKP.tss_GetGames()
                ddlGameID.DataBind()

                ddlSystemID.AutoPostBack = False
                ddlSystemID.DataTextField = "SystemName"
                ddlSystemID.DataValueField = "SystemID"
                ddlSystemID.DataSource = tss_DKP.tss_GetSystems()
                ddlSystemID.DataBind()

                pnlEditPanelForm.Visible = True

            End If

            txtBoardName.Text = Null.NullString
            ddlGameID.ClearSelection()
            ddlSystemID.ClearSelection()
            txtGuildName.Text = Null.NullString
            ddlRegionName.ClearSelection()
            txtRealmName.Text = Null.NullString
            txtGoodAttendanceCutOff.Text = Null.NullString
            txtBadAttendanceCutOff.Text = Null.NullString
            txtPlayerSyncCutOff.Text = Null.NullString

            lblWarning.Text = Null.NullString

            If (PageState = PageState.Add) Then

                If (ddlGameID.Items.Count > 0) Then
                    ddlGameID.Items(0).Selected = True
                    ManageRealmDisplay()
                End If

                txtBoardName.Focus()

            ElseIf (PageState = PageState.SingleUpdate) Then

                pnlRegionName.Visible = False

                txtBoardName.Focus()

            End If

        End Sub

        Private Sub ManageRealmDisplay()

            If (ddlGameID.SelectedItem.Text = "World of Warcraft") Then
                pnlRegionName.Visible = True
            Else
                pnlRegionName.Visible = False
            End If

        End Sub

        Private Sub CheckDummyBoard()

            Dim tss_DKP As New tss_DataController

            Dim aryExistingBoards As ArrayList = tss_DKP.tss_GetBoards(-1)

            If (aryExistingBoards.Count > 0) Then

                Dim objExistingBoard As tss_BoardInfo = aryExistingBoards.Item(0)

                If Not Null.IsNull(Settings("Game")) Then
                    objExistingBoard.GameID = CInt(Settings("Game"))
                End If

                If Not Null.IsNull(Settings("System")) Then
                    objExistingBoard.SystemID = CInt(Settings("System"))
                End If

                If Not Null.IsNull(Settings("Guild")) Then
                    objExistingBoard.GuildName = Convert.ToString(Settings("Guild"))
                End If

                If Not Null.IsNull(Settings("Region")) Then
                    objExistingBoard.RegionName = Convert.ToString(Settings("Region"))
                End If

                If Not Null.IsNull(Settings("Realm")) Then
                    objExistingBoard.RealmName = Convert.ToString(Settings("Realm"))
                End If

                If Not Null.IsNull(Settings("AttendanceGood")) Then
                    objExistingBoard.GoodAttendanceCutOff = CInt(Settings("AttendanceGood"))
                End If

                If Not Null.IsNull(Settings("AttendanceBad")) Then
                    objExistingBoard.BadAttendanceCutOff = CInt(Settings("AttendanceBad"))
                End If

                If Not Null.IsNull(Settings("PlayerSync")) Then
                    objExistingBoard.PlayerSyncCutOff = CInt(Settings("PlayerSync"))
                End If

                objExistingBoard.PortalID = Me.ModuleConfiguration.PortalID

                Try
                    tss_DKP.tss_UpdateBoard(objExistingBoard)
                Catch ex As Exception
                End Try

            End If

        End Sub

#End Region

#Region "Private Event Handlers"

        Private Sub ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles grdBoards.ItemDataBound

            If (e.Item.ItemType.Equals(ListItemType.Item)) Or (e.Item.ItemType.Equals(ListItemType.AlternatingItem)) Then

                Dim lblBoardID As Label = e.Item.FindControl("lblBoardID")
                Dim lblBoardName As Label = e.Item.FindControl("lblBoardName")
                Dim lblGameName As Label = e.Item.FindControl("lblGameName")
                Dim lblGuildName As Label = e.Item.FindControl("lblGuildName")

                Dim objBoard As tss_BoardInfo = e.Item.DataItem

                lblBoardID.Text = objBoard.BoardID.ToString()
                lblBoardID.Visible = False

                lblBoardName.Text = objBoard.BoardName
                lblGameName.Text = objBoard.GameName
                lblGuildName.Text = objBoard.GuildName

            End If

        End Sub

        Private Sub ItemCreated(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles grdBoards.ItemCreated

            If (e.Item.ItemType.Equals(ListItemType.Header)) Then

                For Each currentCell As TableCell In e.Item.Cells
                    currentCell.Attributes.Add("class", "dkpdnn_GridHeader" + grdBoards.Columns(e.Item.Cells.GetCellIndex(currentCell)).FooterText)
                Next

            ElseIf (e.Item.ItemType.Equals(ListItemType.Item)) Then

                e.Item.Attributes.Add("class", "dkpdnn_GridItem")
                e.Item.Attributes.Add("onmouseover", "className='dkpdnn_GridItemMouseOver';")
                e.Item.Attributes.Add("onmouseout", "className='dkpdnn_GridItem';")

                For Each currentCell As TableCell In e.Item.Cells
                    currentCell.Attributes.Add("class", "dkpdnn_GridItem" + grdBoards.Columns(e.Item.Cells.GetCellIndex(currentCell)).FooterText)
                Next

            ElseIf (e.Item.ItemType.Equals(ListItemType.AlternatingItem)) Then

                e.Item.Attributes.Add("class", "dkpdnn_GridAltItem")
                e.Item.Attributes.Add("onmouseover", "className='dkpdnn_GridAltItemMouseOver';")
                e.Item.Attributes.Add("onmouseout", "className='dkpdnn_GridAltItem';")

                For Each currentCell As TableCell In e.Item.Cells
                    currentCell.Attributes.Add("class", "dkpdnn_GridAltItem" + grdBoards.Columns(e.Item.Cells.GetCellIndex(currentCell)).FooterText)
                Next

            ElseIf (e.Item.ItemType.Equals(ListItemType.Footer)) Then

                e.Item.Visible = False

            End If

        End Sub

        Private Sub SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlPageSize.SelectedIndexChanged, ddlGameID.SelectedIndexChanged

            If (sender.Equals(ddlPageSize)) Then

                vsPageSize = ddlPageSize.SelectedValue
                ManagePageState(PageState.View)

            ElseIf (sender.Equals(ddlGameID)) Then

                ManageRealmDisplay()

            End If

        End Sub

        Private Sub SortCommand(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs) Handles grdBoards.SortCommand

            If (vsSortExpression = e.SortExpression) Then
                If (vsSortDirection = "ASC") Then
                    vsSortDirection = "DESC"
                ElseIf (vsSortDirection = "DESC") Then
                    vsSortDirection = "ASC"
                End If
            Else
                vsSortExpression = e.SortExpression
                vsSortDirection = "ASC"
            End If

            ManagePageState(PageState.View)

        End Sub

        Private Sub Click(ByVal sender As Object, ByVal e As EventArgs) Handles lnkFirstPage.Click, lnkPreviousPage.Click, lnkNextPage.Click, _
            lnkLastPage.Click, lnkAdd.Click, lnkUpdate.Click, lnkDelete.Click, lnkSubmit.Click, lnkCancel.Click

            Dim tss_DKP As New tss_DataController

            lblWarning.Text = Null.NullString

            If (sender.Equals(lnkFirstPage)) Then

                vsCurrentPage = 1
                ManagePageState(PageState.View)

            ElseIf (sender.Equals(lnkPreviousPage)) Then

                vsCurrentPage = CInt(vsCurrentPage) - 1
                ManagePageState(PageState.View)

            ElseIf (sender.Equals(lnkNextPage)) Then

                vsCurrentPage = CInt(vsCurrentPage) + 1
                ManagePageState(PageState.View)

            ElseIf (sender.Equals(lnkLastPage)) Then

                vsCurrentPage = vsTotalPages
                ManagePageState(PageState.View)

            ElseIf (sender.Equals(lnkAdd)) Then

                ManagePageState(PageState.Add)

            ElseIf (sender.Equals(lnkUpdate)) Then

                Dim boolIsChecked As Boolean = False

                For Each currentBoard As DataGridItem In grdBoards.Items

                    If (currentBoard.ItemType.Equals(ListItemType.Item)) Or (currentBoard.ItemType.Equals(ListItemType.AlternatingItem)) Then

                        Dim chkBoard As CheckBox = currentBoard.FindControl("chkBoard")
                        Dim lblBoardID As Label = currentBoard.FindControl("lblBoardID")

                        If (chkBoard.Checked) And (boolIsChecked) Then

                            ManagePageState(PageState.View)
                            lblWarning.Text = "Error: Update can only change one board at a time."
                            Exit For

                        ElseIf (chkBoard.Checked) And Not (boolIsChecked) Then

                            boolIsChecked = True

                            ManagePageState(PageState.SingleUpdate)

                            Dim objExistingBoard As tss_BoardInfo = tss_DKP.tss_GetBoard(CInt(lblBoardID.Text))

                            If (objExistingBoard Is Nothing) Then
                                ManagePageState(PageState.View)
                                lblWarning.Text = "Error: Board in selection not found."
                                Exit For
                            Else
                                txtBoardName.Text = objExistingBoard.BoardName
                                ddlGameID.SelectedValue = objExistingBoard.GameID
                                ddlSystemID.SelectedValue = objExistingBoard.SystemID
                                txtGuildName.Text = objExistingBoard.GuildName
                                ddlRegionName.SelectedValue = objExistingBoard.RegionName
                                txtRealmName.Text = objExistingBoard.RealmName
                                txtGoodAttendanceCutOff.Text = objExistingBoard.GoodAttendanceCutOff
                                txtBadAttendanceCutOff.Text = objExistingBoard.BadAttendanceCutOff
                                txtPlayerSyncCutOff.Text = objExistingBoard.PlayerSyncCutOff

                                ManageRealmDisplay()

                            End If

                        End If

                    End If

                Next

                If Not (boolIsChecked) Then
                    ManagePageState(PageState.View)
                End If

            ElseIf (sender.Equals(lnkDelete)) Then

                For Each currentBoard As DataGridItem In grdBoards.Items

                    If (currentBoard.ItemType.Equals(ListItemType.Item)) Or (currentBoard.ItemType.Equals(ListItemType.AlternatingItem)) Then

                        Dim chkBoard As CheckBox = currentBoard.FindControl("chkBoard")
                        Dim lblBoardID As Label = currentBoard.FindControl("lblBoardID")

                        If (chkBoard.Checked) Then
                            tss_DKP.tss_DeleteBoard(CInt(lblBoardID.Text))
                        End If

                    End If

                Next

                ManagePageState(PageState.View)

            ElseIf (sender.Equals(lnkSubmit)) Then

                If (vsPageState = PageState.Add) Then

                    If (txtBoardName.Text.Trim.Length = 0) Then
                        lblWarning.Text = "Error: Board name can not be empty."
                    ElseIf (txtGuildName.Text.Trim.Length = 0) Then
                        lblWarning.Text = "Error: Guild name can not be empty."
                    ElseIf (txtGoodAttendanceCutOff.Text.Trim.Length = 0) Then
                        lblWarning.Text = "Error: Good attendance cut off can not be empty."
                    ElseIf (txtBadAttendanceCutOff.Text.Trim.Length = 0) Then
                        lblWarning.Text = "Error: Bad attendance cut off can not be empty."
                    ElseIf (txtPlayerSyncCutOff.Text.Trim.Length = 0) Then
                        lblWarning.Text = "Error: Player sync cut off can not be empty."
                    Else

                        Dim objNewBoard As New tss_BoardInfo : objNewBoard = CBO.InitializeObject(objNewBoard, GetType(tss_BoardInfo))

                        objNewBoard.BoardName = txtBoardName.Text.Trim
                        objNewBoard.GameID = ddlGameID.SelectedValue
                        objNewBoard.SystemID = ddlSystemID.SelectedValue
                        objNewBoard.GuildName = txtGuildName.Text.Trim
                        objNewBoard.RegionName = ddlRegionName.SelectedValue
                        objNewBoard.RealmName = txtRealmName.Text.Trim
                        objNewBoard.GoodAttendanceCutOff = txtGoodAttendanceCutOff.Text.Trim
                        objNewBoard.BadAttendanceCutOff = txtBadAttendanceCutOff.Text.Trim
                        objNewBoard.PlayerSyncCutOff = txtPlayerSyncCutOff.Text.Trim
                        objNewBoard.PortalID = Me.ModuleConfiguration.PortalID

                        Try
                            tss_DKP.tss_AddBoard(objNewBoard)
                        Catch ex As ApplicationException
                            lblWarning.Text = "Error: Trying to add board resulted in the following error (" + ex.Message + ")."
                        End Try

                    End If

                    If (lblWarning.Text = Null.NullString) Then
                        ManagePageState(PageState.View)
                    End If

                ElseIf (vsPageState = PageState.SingleUpdate) Then

                    For Each currentBoard As DataGridItem In grdBoards.Items

                        If (currentBoard.ItemType.Equals(ListItemType.Item)) Or (currentBoard.ItemType.Equals(ListItemType.AlternatingItem)) Then

                            Dim chkBoard As CheckBox = currentBoard.FindControl("chkBoard")
                            Dim lblBoardID As Label = currentBoard.FindControl("lblBoardID")

                            If (chkBoard.Checked) Then

                                Dim objExistingBoard As tss_BoardInfo = tss_DKP.tss_GetBoard(CInt(lblBoardID.Text))

                                If (objExistingBoard Is Nothing) Then
                                    lblWarning.Text = "Error: Trying to update board resulted in the following error (Board in selection not found.)."
                                Else

                                    objExistingBoard.BoardName = txtBoardName.Text.Trim
                                    objExistingBoard.GameID = ddlGameID.SelectedValue
                                    objExistingBoard.SystemID = ddlSystemID.SelectedValue
                                    objExistingBoard.GuildName = txtGuildName.Text.Trim
                                    objExistingBoard.RegionName = ddlRegionName.SelectedValue
                                    objExistingBoard.RealmName = txtRealmName.Text.Trim
                                    objExistingBoard.GoodAttendanceCutOff = txtGoodAttendanceCutOff.Text.Trim
                                    objExistingBoard.BadAttendanceCutOff = txtBadAttendanceCutOff.Text.Trim
                                    objExistingBoard.PlayerSyncCutOff = txtPlayerSyncCutOff.Text.Trim

                                    Try
                                        tss_DKP.tss_UpdateBoard(objExistingBoard)
                                    Catch ex As ApplicationException
                                        lblWarning.Text = "Error: Trying to update board resulted in the following error (" + ex.Message + ")."
                                    End Try

                                End If

                            End If

                        End If

                    Next

                    If (lblWarning.Text = Null.NullString) Then
                        ManagePageState(PageState.View)
                    End If

                End If

            ElseIf (sender.Equals(lnkCancel)) Then

                ManagePageState(PageState.View)

            End If

        End Sub

#End Region

#Region "Public Methods"

        Public Sub Initialize()

            CheckDummyBoard()

            ManagePageState(PageState.View)

        End Sub

#End Region

    End Class

End Namespace
